import { defineStore } from 'pinia'
interface Tab {
  name: string
  path: string
}
interface State {
  menuCollapsed: boolean
  navTabs: Tab[]
}
const useBaseStore = defineStore('base', {
  state: (): State => {
    return {
      // 侧边菜单折叠状态
      menuCollapsed: false,
      // navTabs数组
      navTabs: []
    }
  },
  actions: {
    // 删除所有
    closeAll() {
      this.navTabs = this.navTabs.filter((item) => item.path === '/dashBoard')
    },
    // 删除除了自己其他tab
    delOther(index: number) {
      this.navTabs = this.navTabs.filter((item, i) => i === index || item.path === '/dashBoard')
    },
    // 删除tab
    delTab(delIndex: number, router: any) {
      this.navTabs.splice(delIndex, 1)
      // 删除后 路由跳转到 tab最后一个对应的路由
      if (this.navTabs.length > 0) {
        router.push(this.navTabs[this.navTabs.length - 1].path)
      }
    },
    addNavTab(tab: Tab) {
      // 新增tab 判断当前 navTabs是否已经有 该tab 有不新增 没有 再新增
      if (!this.navTabs.find((item) => item.path === tab.path)) {
        // 新增
        this.navTabs.push(tab)
      }
    },
    toggleMenuCollapsed() {
      this.menuCollapsed = !this.menuCollapsed
    }
  },
  persist: {
    storage: sessionStorage
  }
})

export default useBaseStore
